<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section class="tb-widget-settings flex flex-col" [formGroup]="persistentTableWidgetSettingsForm">
  <fieldset class="fields-group">
    <legend class="group-title" translate>widgets.persistent-table.general-settings</legend>
    <section class="flex flex-col gt-xs:flex-row gt-xs:gap-2">
      <section class="flex flex-1 flex-col">
        <mat-checkbox formControlName="enableFilter">
          {{ 'widgets.persistent-table.enable-filter' | translate }}
        </mat-checkbox>
      </section>
      <section class="flex flex-1 flex-col">
        <mat-checkbox formControlName="enableStickyHeader">
          {{ 'widgets.persistent-table.enable-sticky-header' | translate }}
        </mat-checkbox>
        <mat-checkbox formControlName="enableStickyAction">
          {{ 'widgets.persistent-table.enable-sticky-action' | translate }}
        </mat-checkbox>
      </section>
    </section>
    <section class="flex flex-col gt-xs:gap-2">
      <mat-slide-toggle class="flex-1" formControlName="displayDetails">
        {{ 'widgets.persistent-table.display-request-details' | translate }}
      </mat-slide-toggle>
      <mat-slide-toggle formControlName="allowSendRequest">
        {{ 'widgets.persistent-table.allow-send-request' | translate }}
      </mat-slide-toggle>
      <mat-slide-toggle formControlName="allowDelete">
        {{ 'widgets.persistent-table.allow-delete-request' | translate }}
      </mat-slide-toggle>
      <div class="tb-form-panel stroked">
        <div class="tb-form-panel-title" translate>widgets.table.pagination</div>
        <mat-slide-toggle class="mat-slide" formControlName="displayPagination">
          {{ 'widgets.table.display-pagination' | translate }}
        </mat-slide-toggle>
        <div class="tb-form-row space-between column-xs">
          <div class="fixed-title-width !min-w-30">{{ 'widgets.table.page-step-settings' | translate }}</div>
          <div class="flex flex-row items-center justify-start gap-2">
            <div class="tb-small-label" translate>widgets.table.page-step-increment</div>
            <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
              <input matInput formControlName="pageStepIncrement" type="number" min="1" step="1" required
                     placeholder="{{ 'widget-config.set' | translate }}">
              <mat-icon matSuffix
                        matTooltipPosition="above"
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="(persistentTableWidgetSettingsForm.get('pageStepIncrement').hasError('required')
                                     ? 'widgets.value-source.value-required'
                                     : 'widgets.table.page-step-increment-format-message') | translate"
                        *ngIf="persistentTableWidgetSettingsForm.get('pageStepIncrement').invalid
                        && persistentTableWidgetSettingsForm.get('pageStepIncrement').touched"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
            <mat-divider vertical class="xs:hidden"></mat-divider>
            <div class="tb-small-label" translate>widgets.table.page-step-count</div>
            <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
              <input matInput formControlName="pageStepCount" type="number" min="1" max="100" step="1" required
                     placeholder="{{ 'widget-config.set' | translate }}">
              <mat-icon matSuffix
                        matTooltipPosition="above"
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="(persistentTableWidgetSettingsForm.get('pageStepCount').hasError('required')
                                     ? 'widgets.value-source.value-required'
                                     : 'widgets.table.page-step-count-format-message') | translate"
                        *ngIf="persistentTableWidgetSettingsForm.get('pageStepCount').invalid
                        && persistentTableWidgetSettingsForm.get('pageStepCount').touched"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
          </div>
        </div>
        <div class="tb-form-row space-between column-xs">
          <div translate>widgets.table.default-page-size</div>
          <mat-form-field appearance="outline" subscriptSizing="dynamic">
            <mat-select formControlName="defaultPageSize" placeholder="{{ 'widget-config.set' | translate }}">
              <mat-option *ngFor="let size of pageStepSizeValues" [value]="size">
                {{ size }}
              </mat-option>
            </mat-select>
          </mat-form-field>
        </div>
      </div>
      <mat-form-field class="mat-block flex-1">
        <mat-label translate>widgets.table.default-sort-order</mat-label>
        <input matInput formControlName="defaultSortOrder">
      </mat-form-field>
    </section>
  </fieldset>
  <fieldset class="fields-group">
    <legend class="group-title" translate>widgets.persistent-table.columns-settings</legend>
    <mat-form-field class="mat-block flex-1">
      <mat-label translate>widgets.persistent-table.display-columns</mat-label>
      <mat-chip-grid #columnsChipList class="tb-drop-list"
                     required
                     cdkDropList
                     cdkDropListOrientation="horizontal"
                     (cdkDropListDropped)="columnDrop($event)">
        <mat-chip-row class="tb-draggable" cdkDrag *ngFor="let column of persistentTableWidgetSettingsForm.get('displayColumns').value"
                  [removable]="true" (removed)="onColumnRemoved(column)">
          {{ displayColumnFromValue(column)?.name }}
          <mat-icon matChipRemove>cancel</mat-icon>
        </mat-chip-row>
        <input matInput type="text" placeholder="{{ 'widgets.persistent-table.column' | translate }}"
               style="max-width: 200px;"
               #columnInput
               (focusin)="onColumnInputFocus()"
               matAutocompleteOrigin
               #origin="matAutocompleteOrigin"
               (input)="columnInputChange.next(columnInput.value)"
               [matAutocompleteConnectedTo]="origin"
               [matAutocomplete]="columnAutocomplete"
               [matChipInputFor]="columnsChipList"
               [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
               (matChipInputTokenEnd)="addColumnFromChipInput($event)">
      </mat-chip-grid>
      <mat-autocomplete #columnAutocomplete="matAutocomplete"
                        class="tb-autocomplete"
                        (optionSelected)="columnSelected($event)"
                        [displayWith]="displayColumnFn">
        <mat-option *ngFor="let column of filteredDisplayColumns | async" [value]="column">
          <span [innerHTML]="column.name | highlight:columnSearchText"></span>
        </mat-option>
        <mat-option *ngIf="(filteredDisplayColumns | async)?.length === 0" [value]="null" class="tb-not-found">
          <div class="tb-not-found-content" (click)="$event.stopPropagation()">
            <div *ngIf="!textIsNotEmpty(columnSearchText); else searchNotEmpty">
              <span translate>widgets.persistent-table.no-columns-found</span>
            </div>
            <ng-template #searchNotEmpty>
                <span>
                  {{ translate.get('widgets.persistent-table.no-columns-matching',
                  {column: truncate.transform(columnSearchText, true, 6, &apos;...&apos;)}) | async }}
                </span>
            </ng-template>
          </div>
        </mat-option>
      </mat-autocomplete>
    </mat-form-field>
  </fieldset>
</section>
